package LeetCode.leetcode;

import domain.ListNode;

/**
 * 两数相加
 * @author: leiwenkang02@meituan.com
 * @date: 2018/8/28
 * @time: 16:42
 */
public class Test02两数相加 {
    public static ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        ListNode result = new ListNode(0);
        ListNode p = result;
        int carry = 0;
        while (l1 != null || l2 != null || carry != 0) {
            if (l1 != null) {
                carry += l1.val;
                l1 = l1.next;
            }

            if (l2 != null) {
                carry += l2.val;
                l2 = l2.next;
            }
            ListNode temp = new ListNode(carry % 10);
            carry /= 10;
            p.next = temp;
            p = temp;

        }
        return result.next;
    }

}
